﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using Forest.Cat.Domain.Services.Authentication;
using Forest.Cat.Admin.Models;
using Forest.Cat.Domain;
using Forest.Cat.Domain.Entities;
using Forest.Cat.Domain.Services.Jobseekers;
using Forest.Cat.Domain.Shared.Security;

namespace Forest.Cat.Admin.Controllers
{
    public class AccountController : Controller
    {
        private readonly IAuthenticationService _authenticationService;
        private readonly IJobseekerRegistrationService _jobseekerRegistrationService;
        private readonly IEncryptionService _encryptionService;
        private readonly IJobseekerService _jobseekerService;
        private readonly IWorkContext _workcontext;

        public AccountController(
            IWorkContext workcontext,
            IAuthenticationService authenticationService,
            IJobseekerRegistrationService jobseekerRegistrationService,
            IEncryptionService encryptionService,
            IJobseekerService jobseekerService)
        {
            _workcontext = workcontext;
            _authenticationService = authenticationService;
            _jobseekerRegistrationService = jobseekerRegistrationService;
            _encryptionService = encryptionService;
            _jobseekerService = jobseekerService;
        }

        //
        // GET: /Account/ChangePassword
        [Authorize]
        public ActionResult ChangePassword()
        {
            return View();
        }

        //
        // POST: /Account/ChangePassword
        [HttpPost]
        public ActionResult ChangePassword(ChangePasswordModel model)
        {
            if (ModelState.IsValid)
            {
                if (_jobseekerRegistrationService.ValidateUser(_workcontext.CurrentUser.Email, model.OldPassword))
                {
                    var jobseeker = _jobseekerService.GetUserByEmail(_workcontext.CurrentUser.Email);
                    var saltKey = _encryptionService.CreateSaltKey(5);
                    var md5Password = _encryptionService.CreatePasswordHash(model.NewPassword, saltKey);

                    jobseeker.PasswordSalt = saltKey;
                    jobseeker.Password = md5Password;

                    _workcontext.CurrentUser.PasswordSalt = saltKey;
                    _workcontext.CurrentUser.Password = md5Password;

                    _jobseekerService.UpdateUser(jobseeker);

                    return RedirectToAction("ChangePasswordSuccess");
                }
                else
                {
                    ModelState.AddModelError("pwdError", "原密码输入错误！");
                }
            }
            return View(model);
        }

        //
        // GET: /Account/ChangePasswordSuccess
        public ActionResult ChangePasswordSuccess()
        {
            return View();
        }

        #region Status Codes
        private static string ErrorCodeToString(MembershipCreateStatus createStatus)
        {
            // 请参见 http://go.microsoft.com/fwlink/?LinkID=177550 以查看
            // 状态代码的完整列表。
            switch (createStatus)
            {
                case MembershipCreateStatus.DuplicateUserName:
                    return "用户名已存在。请输入不同的用户名。";

                case MembershipCreateStatus.DuplicateEmail:
                    return "该电子邮件地址的用户名已存在。请输入不同的电子邮件地址。";

                case MembershipCreateStatus.InvalidPassword:
                    return "提供的密码无效。请输入有效的密码值。";

                case MembershipCreateStatus.InvalidEmail:
                    return "提供的电子邮件地址无效。请检查该值并重试。";

                case MembershipCreateStatus.InvalidAnswer:
                    return "提供的密码取回答案无效。请检查该值并重试。";

                case MembershipCreateStatus.InvalidQuestion:
                    return "提供的密码取回问题无效。请检查该值并重试。";

                case MembershipCreateStatus.InvalidUserName:
                    return "提供的用户名无效。请检查该值并重试。";

                case MembershipCreateStatus.ProviderError:
                    return "身份验证提供程序返回了错误。请验证您的输入并重试。如果问题仍然存在，请与系统管理员联系。";

                case MembershipCreateStatus.UserRejected:
                    return "已取消用户创建请求。请验证您的输入并重试。如果问题仍然存在，请与系统管理员联系。";

                default:
                    return "发生未知错误。请验证您的输入并重试。如果问题仍然存在，请与系统管理员联系。";
            }
        }
        #endregion
    }
}
